<template>
	<view>
		<navbar title="积分商城" @goBack="goBack"></navbar>
		<image src="/static/owo/user_bg.png" class="bg" mode="widthFix"></image>
		<view class="usercontent">
			<view class="limg">
				<image class="avatar" :src="user.avatar">
				</image>
			</view>
			<view class="rightc">
				<view class="currenttxt">当前积分<image src="/static/owo/yiwen.png" class="yiwen"></image>
				</view>
				<text class="ticket">{{wallet.ticket}}</text>
			</view>
			<text class="record">积分记录</text>
		</view>
		<view class="tap"></view>
		<view class="nav">
			<view class="txt" @click="tabChange('redeem')" :class="[activeTab=='redeem'?'select':'']">积分兑换
			</view>
			<view class="txt" @click="tabChange('prizeadv')" :class="[activeTab=='prizeadv'?'select':'']">奖品进阶
			</view>
		</view>
		<view class="products-list" v-if="activeTab=='redeem'">
			<view v-for="(e, index) in goods" :key="index" class="product-item" @click="gotoDetail(e,1)">
				<image :src="e.main_photo" class="goodsimg"></image>
				<text class="goodsname">{{e.name}}</text>
				<view class="content">
					<text class="goodsnum"><text class="goodspoint">{{e.price}}</text>积分</text>
					<text class="duihuan">兑换</text>
				</view>
			</view>
		</view>
		<view class="products-list1" v-if="activeTab=='prizeadv'">
			<view v-for="(e, index) in goods" :key="index" class="product-item" @click="gotoDetail(e,2)">
				<image :src="e.main_photo" class="goodsimg"></image>
				<view class="rightc">
					<text class="goodsname">{{e.name}}</text>
					<text class="goodsnum"><text class="goodspoint">{{e.price}}</text>积分</text>
					<view class="content">
						<text class="precenttxt">已开始：30%</text>
						<text class="duihuan">兑换</text>
					</view>
					<view class="precent">
						<view class="innerprecent">
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup lang="ts">
	import navbar from '@/wxcomponents/navbar'
	import {
		onLoad,
		onReachBottom,
		onPullDownRefresh,
		onShow
	} from "@dcloudio/uni-app"
	import {
		ref,
		reactive,
		defineProps,
		onUnmounted,
		computed
	} from 'vue'
	import {
		listYiFanShang
	} from '@/api/yifanshang';
	import {
		loadWallet,
		loadUserInfo
	} from '@/api/user'
	let activeTab = ref('prizeadv');
	let goods = ref([]);
	let user = ref({
		avatar: "/static/owo/avatar.png",
		nickname: "",
		referenceCode: "",
		token: ""
	})
	let wallet = ref({
		ticket: ""
	})

	function goBack() {
		uni.navigateBack(-1)
	}

	function gotoDetail(e,type) {
		uni.navigateTo({
			url: '/pages/subPackage/malldetail?id=' + e.id+"&type="+type
		})

	}
	onLoad(() => {
		getlistYiFanShang()
	})
	onShow(() => {
		getUserInfo()
		getloadWallet()
	})
	// tab切换
	async function tabChange(tabName) {
		activeTab.value = tabName
		if (tabName === "all") {
		} else {

		}
		getlistYiFanShang();
	}
	async function getlistYiFanShang() {
		const params = {
			'type': '一番赏',
			'index': 1
		}

		const res = await listYiFanShang(params)
		const data = res.data || {}
		const items = data.items || []

		for (let i = 0; i < items.length; i++) {
			items[i]['price'] = (Number(items[i]['price']) / 100).toFixed(2)
		}
		goods.value = items
	}
	async function getUserInfo() {
		const token = uni.getStorageSync("token")
		let userInfo = {}
		if (token) {
			const userRes = await loadUserInfo()
			userInfo = userRes.data || {}
		}

		const avatar = userInfo.avatar || ''
		const userId = userInfo.user_id || ''
		const nickname = userInfo.nickname || ''
		const referenceCode = userInfo.reference_code || ''
		uni.setStorageSync('avatar', avatar)
		uni.setStorageSync('userId', userId)
		uni.setStorageSync('nickname', nickname)
		uni.setStorageSync('referenceCode', referenceCode)

		user.value = {
			avatar: avatar,
			nickname: nickname,
			referenceCode: referenceCode,
			token: token
		}
	}
	async function getloadWallet() {
		const token = uni.getStorageSync("token")
		let score = 0
		let ticket = 0
		if (token) {
			const res = await loadWallet()
			const data = res.data || {}
			score = data.score || 0
			ticket = data.ticket || 0
		}

		wallet.value = {
			balance: 0,
			score: score,
			ticket: ticket,
		}
	}
</script>

<style lang="scss">
	page {
		background: #18181C;
	}
	.products-list1 {
		padding: 0 32rpx 32rpx;
		display: flex;
		justify-content: flex-start;
		flex-direction: row;
		flex-wrap: wrap;
		box-sizing: border-box;
		.product-item {
			width: 100%;
			margin-bottom: 4%;
			min-height: 302rpx;
			background-image: url("/static/owo/goodsbg3.png");
			background-position: center;
			background-repeat: no-repeat;
			background-size: 100%;
			padding:0 24rpx;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
		
			.goodsimg {
				width: 252rpx;
				height: 252rpx;
			}
			.rightc {
				flex: 1;
				padding: 24rpx;
				height: 252rpx;
				display: flex;
				justify-content: space-between;
				align-items: flex-start;
				flex-direction: column;
			}
			.goodsname {
				color: #ffffff;
				font-size: 24rpx;
				text-align: left;
				display: inline-block;
				margin: 20rpx 0;
			}
			.goodsnum {
				font-size: 20rpx;
				display: flex;
				align-items: flex-end;
				flex-direction: row;
				color: rgba(255, 255, 255, 0.65);
					
				.goodspoint {
					font-size: 36rpx;
					color: #FFDD86;
					padding-right: 6rpx;
				}
			}
			.content {
				width: 100%;
				text-align: left;
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				align-items: flex-end;
				margin-top: 10rpx;
				.precenttxt {
					color: #ffffff;
					font-size: 22rpx
				}
		
				.duihuan {
					height: 32rpx;
					line-height: 32rpx;
					display: inline-block;
					padding: 0rpx 12rpx;
					font-size: 22rpx;
					color: #18181C;
					background: linear-gradient(to right, rgba(255, 247, 194, 1), rgba(255, 209, 99, 1));
				}
			}
			.precent {
				width: 100%;
				border: solid 1rpx rgba(255, 255, 253, 0.31);
				height: 14rpx;
				display: flex;
				align-items: center;
				padding-left: 2rpx;
				flex-direction: row;
				margin-bottom: 8rpx;
				.innerprecent {
					width: 60%;
					height: 6rpx;
					background: linear-gradient(to right,rgba(255, 245, 218, 1),rgba(255, 221, 134, 1));
				}
			}
		}
	}
	.products-list {
		padding: 0 32rpx 32rpx;
		display: flex;
		justify-content: flex-start;
		flex-direction: row;
		flex-wrap: wrap;
		box-sizing: border-box;

		.product-item {
			width: 48%;
			box-sizing: border-box;
			margin-right: 4%;
			margin-bottom: 4%;
			min-height: 460rpx;
			background-image: url("/static/owo/goodsbg2.png");
			background-position: center;
			background-repeat: no-repeat;
			background-size: 100%;
			padding: 24rpx;
			display: flex;
			flex-direction: column;
			align-items: center;

			.goodsimg {
				width: 274rpx;
				height: 274rpx;
				margin-bottom: 24rpx;
				margin-top: 14rpx;
			}

			.goodsname {
				color: #ffffff;
				text-overflow: ellipsis;
				overflow: hidden;
				word-break: break-all;
				white-space: nowrap;
				font-size: 24rpx;
				text-align: left;
				display: inline-block;
				width: 274rpx;
			}

			.content {
				width: 274rpx;
				text-align: left;
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				align-items: center;
				margin-top: 10rpx;

				.goodsnum {
					font-size: 20rpx;
					display: flex;
					align-items: flex-end;
					flex-direction: row;
					color: rgba(255, 255, 255, 0.65);

					.goodspoint {
						font-size: 36rpx;
						color: #FFDD86;
						padding-right: 6rpx;
					}
				}

				.duihuan {
					height: 32rpx;
					line-height: 32rpx;
					display: inline-block;
					padding: 0rpx 12rpx;
					font-size: 22rpx;
					color: #18181C;
					background: linear-gradient(to right, rgba(255, 247, 194, 1), rgba(255, 209, 99, 1));
				}
			}
		}

		.product-item:nth-child(2n+2) {
			margin-right: 0%;
		}
	}

	.tap {
		width: 700rpx;
		margin: 10rpx auto;
		border-top: solid 2rpx rgba(255, 255, 255, 0.20);
	}

	.usercontent {
		width: 100%;
		padding: 24rpx 24rpx 48rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		box-sizing: border-box;
		position: relative;

		.limg {
			margin-right: 28rpx;
			width: 92rpx;
			height: 92rpx;
			box-sizing: border-box;
			border-radius: 50%;
			border: solid 4rpx #ffffff;

			.avatar {
				width: 100%;
				height: 100%;
			}
		}

		.rightc {
			display: flex;
			flex-direction: column;
			align-items: flex-start;

			.currenttxt {
				display: flex;
				flex-direction: row;
				align-items: center;
				font-size: 24rpx;
				color: rgba(255, 255, 255, 0.85);
			}

			.yiwen {
				width: 24rpx;
				height: 24rpx;
				margin-left: 6rpx;
			}

			.ticket {
				color: #ffffff;
				font-weight: bold;
				font-size: 56rpx;
			}
		}

		.record {
			position: absolute;
			right: 24rpx;
			top: 50%;
			transform: translateY(-50%);
			display: inline-block;
			padding: 10rpx 12rpx;
			color: #FFDD86;
			font-size: 24rpx;
			border: dashed 2rpx rgba(255, 255, 253, 0.31);
		}
	}

	.bg {
		width: 100%;
		max-height: 350rpx;
		position: absolute;
		top: 0;
		left: 0;
		z-index: -1;
	}
	.nav {
		display: flex;
		justify-content: flex-start;
		margin-top: 20rpx;
		padding: 32rpx;

		.txt {
			display: inline-block;
			width: 132rpx;
			height: 44rpx;
			line-height: 44rpx;
			text-align: center;
			position: relative;
			color: #ffffff;
			font-size: 28rpx;
			margin-right: 24rpx;
		}

		.select {
			background-image: url("/static/owo/navselect.png");
			background-position: 0 0;
			background-repeat: no-repeat;
			background-size: 100%;
			color: rgba(255, 221, 134, 1);
		}
	}
	.product-item {
		display: flex;
		justify-content: flex-start;
	}
</style>